<?php
/**
 * @page tabbed_content_module Tabbed Content Module
 * 
 * This module is same as @ref simple_content_module except that it outputs the content in tabs. So you can
 * create, say, 3 tabs like: Top Hotels, Top Bars, Top Users or something like this:
 * 
 * @image html tabbed_content_module.jpg
 * 
 */
class TabbedContentBoxModule extends GigaModule
{
	var $name = 'Tabbed Content Box';
	
	var $configuration = array('conditions' => array('type' => 'parent', 
			'childInfo' => array(
				'model' => 'ModuleTab',
				'child_group' => 'add',
				'allowMultiple' => true
			),
			'ignoreCallback' => true,
			'label'=>"",
			'group' => 'add',
		),
		'view_type'=>array(
			'label'=>'Display Style',
			'type'=>'radio',
			'options'=>array(
				'table_view'=>'Tableview',
				'blog_view'=>'Blogview'
				),
			'default'=>'table_view',
			'group'=>"add",
		),);
	function __construct()
	{
		$this->pageProperties['after'] = '<LINK href="'.cr('cmsConfig.Backend.siteUrl').'/cms/horizontalInput/4.css" rel="stylesheet" type="text/css">';
	}
	
	function render($params)
	{
		$content = null;
		$viewObj = ClassRegistry::getObject('view');
		$html = $viewObj->loaded['html'];
		$html->initHelper('Cms.Tabs');
		$tabs = $html->Tabs;
		$viewType = $params['view_type'];
		$out = false;
		if(is_array($params['conditions']))
		{
			foreach($params['conditions'] as $newTabParam)
			{
				$tabHtml = $this->getTypeContent($newTabParam,$viewType);
				if(is_string($tabHtml))
				{
					$tabs->addTab($newTabParam['title'],$tabHtml );
				}
			}
			$out = $tabs->show(array('output_js' => true,'container_id'=>"module_tabbed_content_".rand(1,1000)));
		}
		return $out;
	}
	
	private function getTypeContent($params,$viewType)
	{
		$title = $params['title'];
		$viewObj = ClassRegistry::getObject('view');
		$limit = cri('ModuleTab')->getFieldProperty('limit','default');
		extract($params);
		$entity_plural = low(Inflector::pluralize($model));
		if(strlen($model)<1)
		{
			return false;
		}
		$params = compact('model','limit','scope','view_type');
		$records = cri('ContentScope')->getContent($params,$viewObj->params);
		if(!is_array($records) || empty($records))
		{
			return false;
		}
		$out = $viewObj->element($entity_plural, array($entity_plural => $records,'view_type'=>$viewType,'isModuleRender'=>true));
		return $out;
	}
}
?>